home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Night Owl 6
/
Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso
/
029a
/
fcopyc10.zip
/
FCOPY.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-01-05
|
7KB
|
117 lines
/* FCOPY.C -- contains _fcopy() function */
/* */
/* This function copies one file to another, much as the DOS COPY */
/* command does. It does not check to see if the two files are */
/* the same, so it is up to the caller to verify that the two file */
/* specs do not, in fact, refer to the same file. */
/* */
/* _fcopy() uses a farmalloc() call to allocate a large file buffer */
/* from the far heap, regardless of the memory model being used. In */
/* order to perform reads and writes to this buffer, new _farread() */
/* and _farwrite() functions were written. The source code for these */
/* functions is contained in FARREAD.ASM. */
/* */
/* BUFF_SIZE, indicating the buffer size, can be any int sized */
/* value up to (but not including) 65,535. It is recommended that */
/* the buffer be a multiple of the sector size (512 bytes in the */
/* current version of DOS), since reads and writes are more efficient */
/* if they involve whole sectors. A good value to use is 65024 (in */
/* decimal), since this is 1 sector (512 bytes) short of a complete */
/* segment (64k bytes). */
/* */
/* Function prototypes are contained in fcopy.h */
/* */
/* Usage: */
/* */
/* #include "fcopy.h" */
/* int _fcopy (char *sourcename, char *destname) */
/* */
/* The function returns 0 if sucessful, -1 otherwise. Error codes */
/* are returned in the global variables errno and _doserrno. */
/* Possible error codes are: */
/* */
/* EINVFNC Invalid function number (1) */
/* ENOFILE File not found (2) */
/* ENOPATH Path not found (3) */
/* EMFILE Too many open files (4) */
/* EACCESS Permission denied (5) */
/* EBADF Bad file number (6) */
/* ECONTR Memory blocks destroyed (7) */
/* ENOMEM Not enough core (8) */
/* EINVMEM Invalid memory block address (9) */
/* EINVACC Invalid access code (12) */
/* -1 Target disk full (-1) */
/* ------------------------------------------------------------------- */
/* Revision history: */
/* */
/* 1.0 5 JAN 92 Original. Written for Turbo C++. */
/* ------------------------------------------------------------------- */
/* Copyright (c) 1992 Ray Waters */
/* All Rights Reserved */
/* ------------------------------------------------------------------- */
#define BUFF_SIZE 65024U /* size of file buffer */
#include <io.h> /* _open, _creat, _close */
#include <fcntl.h> /* definition for O_RDONLY */
#include <alloc.h> /* farmalloc, farfree, NULL pointer */
#include "fcopy.h" /* _fcopy, _farread, _farwrite */
void __cleanup(void); /* function to close files, free memory */
char far *buffer;
int SrcHandle, DestHandle;
int _fcopy(const char *sourcename, const char *destname)
{
register int BytesRead;
struct ftime ft;
SrcHandle = _open(sourcename, O_RDONLY); /* open source (read only) */
if (SrcHandle == -1) /* if open failed, */
return -1; /* return error */
if (getftime(SrcHandle, &ft)) { /* get date/time stamp */
_close(SrcHandle); /* if error, close file */
return -1; /* return error */
}
DestHandle = _creat(destname, 0); /* create/truncate target */
if (DestHandle == -1) { /* if open failed, */
_close(SrcHandle); /* close source file */
return -1; /* return error */
}
if (!(buffer = farmalloc(BUFF_SIZE))) { /* allocate a far buffer */
/* if allocation failed, */
_close(SrcHandle); /* close source file */
_close(DestHandle); /* close target file */
return -1; /* return error */
}
do { /* read from source */
BytesRead = _farread(SrcHandle, buffer, BUFF_SIZE);
if (BytesRead == -1) { /* if read failed, */
__cleanup(); /* close files, free mem */
return -1; /* return error */
}
/* write to target */
if (_farwrite(DestHandle, buffer, BytesRead) == -1) {
/* if write failed, */
__cleanup(); /* close files, free mem */
return -1; /* return error */
}
}
while (BytesRead); /* loop until finished */
setftime(DestHandle, &ft); /* set target date/time stamp */
__cleanup(); /* close files, free mem */
return 0; /* return success */
}
void __cleanup(void) /* close files and release memory */
{
_close(SrcHandle); /* close source file */
_close(DestHandle); /* close target file */
farfree(buffer); /* free memory */
}